# !/usr/bin/env python
# _*_ coding: utf-8 _*_
"""
Time      : 2019/12/20 11:07
@Author   : gao
@Site     : 
@File     : sysconvert_util.py
@Software : PyCharm
"""

# 进制转换工具类
base = [str(x) for x in range(10)] + [chr(x) for x in range(ord('A'), ord('A') + 6)]


# hex2dec
def hex2dec(string_num):
    if string_num:
        return str(float(int(string_num.upper(), 16)))


def bin2dec(string_num):
    if string_num:
        return str(int(string_num, 2))


def dec2hex(string_num):
    num = int(string_num)
    mid = []
    while True:
        if num == 0: break
        num, rem = divmod(num, 16)
        mid.append(base[rem])

    return ''.join([str(x) for x in mid[::-1]])


def dec2bin(string_num):
    num = int(string_num)
    mid = []
    while True:
        if num == 0: break
        num, rem = divmod(num, 2)
        mid.append(base[rem])
    return ''.join([str(x) for x in mid[::-1]])


def hex2bin(string_num):
    return dec2bin(hex2dec(string_num.upper()))


def bin2hex(string_num):
    return dec2hex(bin2dec(string_num))
